package index

import (
	"beego-admin/global"
	_ "beego-admin/models"
	"beego-admin/services"
	_ "beego-admin/services"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego/cache"
	_ "github.com/astaxie/beego/cache/redis"
	beego "github.com/beego/beego/v2/adapter"
	_ "github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"
	_ "net"
	"time"
)

// IndexController struct
type IndexController struct {
	beego.Controller
}

type LIKE struct {
	Food   string
	Watch  string
	Listen string
}

type JSONS struct {
	//必须的大写开头
	Code string
	Msg  string
	User []string `json:"user_info"` //key重命名,最外面是反引号
	Like LIKE
}

// 存放文章信息的 Post 结构体
type Book struct {
	Id      int    `json:"id"`
	Title   string `json:"title"`
	Summary string `json:"summary"`
	Author  string `json:"author"`
}

type Emnv struct {
	One   int `json:"one"`
	Two   int `json:"two"`
	Three int `json:"three"`
}

// Get 显示
func (c *IndexController) Get() {

	//protobuf用法
	/*text :=&protobufapi.Test{
		Name :"panda",
		Tizhong :[]int32{120,125,198,180,150,195},
		Shengao :180,
		Motto:"天生我才必有用",
	}
	fmt.Println(text)
	dataes,err :=proto.Marshal(text)
	if err !=nil{
		fmt.Println("编译失败")
	}
	fmt.Println(123456,dataes)

	newtext := &protobufapi.Test{}
	err = proto.Unmarshal(dataes,newtext)
	if err !=nil{
		fmt.Println("解码失败")
	}
	//解码后打印
	fmt.Println(newtext)
	fmt.Println(newtext.Motto)
	fmt.Println(newtext.Tizhong)*/

	//接口的书写
	//data := &JSONS{Code: "100",Msg: "获取成功",
	//	User:[]string{"maple","18"},Like:LIKE{"蛋糕","电影","音乐"}}
	//c.Data["json"] = data
	//c.ServeJSON()

	//var lists []orm.ParamsList
	//执行原生sql
	//var lists []orm.Params
	//ids := []int{1, 2, 3}
	//num , err:=orm.NewOrm().Raw("SELECT * FROM attachment WHERE id IN (?, ?, ?)", ids).Values(&lists)
	//num , err:=orm.NewOrm().Raw("SELECT * FROM attachment WHERE id >0").Values(&lists)
	//if err == nil && num > 0 {
	//	fmt.Println(lists)
	//	c.Data["json"] = lists
	//	c.ServeJSON()
	//}

	//链接数据库
	//内置方法增删查改
	var UserProfileService services.UserProfileService

	// 初始化一个字典类型变量
	// books := make(map[int]*Book)
	//book1 := Book{Id: 1, Title: "Go Web 编程", Summary: "Go Web 编程入门指南", Author: "学院君"}
	////books[book1.Id] = &book1
	//
	JSONS := Emnv{One: 0, Two: 1, Three: 0}

	//数组里面存结构体
	values := []interface{}{JSONS, JSONS, JSONS}
	//数组里面存集合
	//oop := []map[string]string{}  创建集合

	countryCapitalMap := []map[string]map[string]string{
		{
			"France": {"France": "Paris"},
			"Italy":  {"France": "Paris"},
		},
	}
	dataS, _ := json.Marshal(countryCapitalMap)
	fmt.Println(string(dataS))
	//fmt.Println(values)

	//// 通过 JSON 序列化字典数据
	data, _ := json.Marshal(values)
	fmt.Println(string(data))
	// 将 JSON 格式数据写入当前目录下的d books 文件（文件不存在会自动创建）
	//err := ioutil.WriteFile("books123", data, 0644)
	//if err != nil {
	//	panic(err)
	//}

	//profile := models.UserProfile{
	//	Name:       "fly",
	//	Age:        12,
	//	Profile:    string(data),
	//	CreateTime: 1629365389,
	//	UpdateTime: 1629365389,
	//	//Posts:'',
	//}
	//
	//////增加数据
	//insertID := UserProfileService.Create(&profile)
	//fmt.Println(insertID)

	//查数据
	//alc := UserProfileService.GetAllAdminUser()

	alc := UserProfileService.GetAllUser()
	fmt.Println(alc)

	//e := l.Front()
	//21     p, ok := (e.Value).(People)

	//for key,value := range alc {
	//	fmt.Printf(value)
	//}

	//接口返回
	//if alc!=nil {
	//	c.Data["json"] = alc
	//	c.ServeJSON()
	//	//response.SuccessWithMessageAndUrl("添加成功", "/index/index/index", c.Ctx)//重定向
	//} else {
	//	response.Error(c.Ctx)
	//}

	//redis缓存用法
	//连接redis
	bytes, _ := json.Marshal(map[string]string{"key": "chatengine", "conn": global.BA_CONFIG.Redis.Addr, "dbNum": global.BA_CONFIG.Redis.DB, "password": global.BA_CONFIG.Redis.Password})
	//初始化redis
	cache_conn, err := cache.NewCache("redis", string(bytes))
	if err != nil {
		fmt.Println(err)
	}
	//存数据
	err = cache_conn.Put("wilson1231111", "廖双飞", time.Second*100)
	if err != nil {
		fmt.Println("数据存取出错，错误为：", err)
	}
	//取数据
	if areaData := cache_conn.Get("wilson1231111"); areaData != nil {
		fmt.Println(string(areaData.([]byte)))
	} else {
		fmt.Println("读取失败")
	}
	c.Data["data"] = alc
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.tpl"

}
